package cn.zmall.manager.dto;

import cn.zmall.common.pojo.TreeNode;
import cn.zmall.manager.dto.front.CartProduct;
import cn.zmall.manager.dto.front.Member;
import cn.zmall.manager.dto.front.Product;
import cn.zmall.manager.pojo.TbItem;
import cn.zmall.manager.pojo.TbItemCat;
import cn.zmall.manager.pojo.TbMember;
import cn.zmall.manager.pojo.TbPanel;

/**
 * @Author BinBini
 * @Date 2020/10/28 10:33
 * @Version 1.0
 */
public class DtoUtil {

    public static Member TbMemberToMember(TbMember tbMember) {
        Member member = new Member();

        member.setId(tbMember.getId());
        member.setUsername(tbMember.getUsername());
        member.setEmail(tbMember.getEmail());
        member.setPhone(tbMember.getPhone());
        member.setSex(tbMember.getSex());
        member.setAddress(tbMember.getAddress());
        member.setFile(tbMember.getFile());
        member.setDescription(tbMember.getDescription());
        member.setPoints(tbMember.getPoints());
        member.setBalance(tbMember.getBalance());

        return member;
    }

    public static TbMember MemberDtoToTbMember(MemberDto memberDto) {
        TbMember tbMember = new TbMember();

        if(!memberDto.getUsername().isEmpty()){
            tbMember.setUsername(memberDto.getUsername());
        }
        if(!memberDto.getPassword().isEmpty()){
            tbMember.setPassword(memberDto.getPassword());
        }
        if(!memberDto.getPhone().isEmpty()){
            tbMember.setPhone(memberDto.getPhone());
        }
        if(!memberDto.getEmail().isEmpty()){
            tbMember.setEmail(memberDto.getEmail());
        }
        if(!memberDto.getSex().isEmpty()){
            tbMember.setSex(memberDto.getSex());
        }
        if(!memberDto.getDescription().isEmpty()){
            tbMember.setDescription(memberDto.getDescription());
        }
        if(!memberDto.getProvince().isEmpty()){
            tbMember.setAddress(memberDto.getProvince()+" "
                    +memberDto.getCity()+" "+memberDto.getDistrict());
        }

        return tbMember;
    }

    public static ItemDto TbItemToItemDto(TbItem tbItem) {
        ItemDto itemDto = new ItemDto();

        itemDto.setTitle(tbItem.getTitle());
        itemDto.setPrice(tbItem.getPrice());
        itemDto.setCid(tbItem.getCid());
        itemDto.setImage(tbItem.getImage());
        itemDto.setSellPoint(tbItem.getSellPoint());
        itemDto.setNum(tbItem.getNum());
        if(tbItem.getLimitNum()==null){
            itemDto.setLimitNum(tbItem.getNum());
        }else if(tbItem.getLimitNum()<0&&tbItem.getNum()<0) {
            itemDto.setLimitNum(10);
        }else{
            itemDto.setLimitNum(tbItem.getLimitNum());
        }

        return itemDto;
    }

    public static TbItem ItemDtoToTbItem(ItemDto itemDto) {
        TbItem tbItem = new TbItem();

        tbItem.setTitle(itemDto.getTitle());
        tbItem.setPrice(itemDto.getPrice());
        tbItem.setCid(itemDto.getCid());
        tbItem.setImage(itemDto.getImage());
        tbItem.setSellPoint(itemDto.getSellPoint());
        tbItem.setNum(itemDto.getNum());
        if(itemDto.getLimitNum()==null||itemDto.getLimitNum()<0){
            tbItem.setLimitNum(10);
        }else{
            tbItem.setLimitNum(itemDto.getLimitNum());
        }

        return tbItem;
    }

    public static CartProduct TbItemToCartProduct(TbItem tbItem) {
        CartProduct cartProduct = new CartProduct();

        cartProduct.setProductId(tbItem.getId());
        cartProduct.setSalePrice(tbItem.getPrice());
        cartProduct.setLimitNum((long) tbItem.getLimitNum());
        cartProduct.setProductName(tbItem.getTitle());
        cartProduct.setProductImg(tbItem.getImage());

        return cartProduct;
    }

    public static Product TbItemToProduct(TbItem tbItem) {
        Product product = new Product();

        product.setProductId(tbItem.getId());
        product.setSalePrice(tbItem.getPrice());
        product.setProductName(tbItem.getTitle());
        product.setSubTitle(tbItem.getSellPoint());
        product.setProductImageBig(tbItem.getImages()[0]);

        return product;
    }

    public static TreeNode TbPanelToTreeNode(TbPanel tbPanel) {
        TreeNode treeNode = new TreeNode();

        treeNode.setId(tbPanel.getId());
        treeNode.setIsParent(false);
        treeNode.setpId(0);
        treeNode.setName(tbPanel.getName());
        treeNode.setSortOrder(tbPanel.getSortOrder());
        treeNode.setStatus(tbPanel.getStatus());
        treeNode.setRemark(tbPanel.getRemark());
        treeNode.setLimitNum(tbPanel.getLimitNum());
        treeNode.setType(tbPanel.getType());

        return treeNode;
    }

    public static TreeNode TbItemCatToTreeNode(TbItemCat tbItemCat) {
        TreeNode treeNode = new TreeNode();

        treeNode.setId(Math.toIntExact(tbItemCat.getId()));
        treeNode.setStatus(tbItemCat.getStatus());
        treeNode.setSortOrder(tbItemCat.getSortOrder());
        treeNode.setName(tbItemCat.getName());
        treeNode.setpId(Math.toIntExact(tbItemCat.getParentId()));
        treeNode.setIsParent(tbItemCat.getIsParent());
        treeNode.setRemark(tbItemCat.getRemark());

        return treeNode;
    }

}
